package com.lhn.controller;

import com.lhn.entity.Menu;
import com.lhn.entity.ResponseMsg;
import com.lhn.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author lhn
 * @date 2024-10-07 22:26
 */

@RestController
@RequestMapping("menu")
public class MenuController {

    @Value("${server.port}")
    private String port;

    @GetMapping("index")
    public String index() {
        return "menu port: " + port;
    }

    @Autowired
    private MenuService menuService;

    @GetMapping("findAll/{index}/{limit}")
    public List<Menu> findAll(@PathVariable("index") int index, @PathVariable("limit") int limit) {
//        System.out.println("this is menu controller findAll");
        return menuService.findAll(index, limit);
    }

    @GetMapping("findAllWithType/{index}/{limit}")
    public List<Menu> findAllWithType(@PathVariable("index") int index, @PathVariable("limit") int limit) {
        return menuService.findAllWithType(index, limit);
    }

    @GetMapping("count")
    public int count() {
        return menuService.count();
    }

    @PostMapping("save")
    public void save(@RequestBody Menu menu) {
        menuService.save(menu);
    }

    @PostMapping("update")
    public void update(@RequestBody Menu menu) {
        menuService.update(menu);
    }

    @GetMapping("deleteById/{id}")
    public void deleteById(@PathVariable("id") long id) {
        menuService.deleteById(id);
    }

    @GetMapping("findById/{id}")
    public Menu findById(@PathVariable("id") long id) {
        ModelAndView modelAndView = new ModelAndView();
        return menuService.findById(id);
    }
}
